-- DungeonGuideM
-- created by zouyb
-- 地牢内引导模块

module("DungeonGuideM", package.seeall);

-- 配置
local guideConfig = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    guideConfig = {};
    local dungeon_guide_CSV = get_dungeon_guide_CSV();
    for i = 1, #dungeon_guide_CSV do
        guideConfig[dungeon_guide_CSV[i].id] = parseConfig(dungeon_guide_CSV[i]);
    end
    dungeon_guide_CSV = {};
end

-- 模块初始化操作
function init()
    loadCsv();
end

-- 解析配置
function parseConfig(info)
    -- 解析触发条件
    local str = info.trigger;
    if str == "" then
        info.trigger = nil;
    else
        local _, _, name, para = string.find(str, "([a-zA-Z_]+)%(([%a%d, ]+)%)");
        if name ~= nil and para ~= nil then
            info.trigger = {name = name, para = para};
        else
            print("parse error! info.id:"..info.id..", str:"..str);
        end
    end

    -- 解析后置函数
    str = info.post_func;
    if str == "" then
        info.post_func = nil;
    else
        local _, _, name, para = string.find(str, "([a-zA-Z_]+)%(([%a%d, ]+)%)");
        if name ~= nil and para ~= nil then
            info.post_func = {name = name, para = para};
        else
            print("parse error! info.id:"..info.id..", str:"..str);
        end
    end

    local msg = info.p1;
    str = info.p3;
    -- 解析显示信息
    if info.action == "show_msg"
        or info.action == "show_mask"
        or info.action == "play_appear_effect"
        or info.action == "play_disappear_effect" then
        -- 解析对话
        if msg ~= "" and msg ~= nil then
            local msgList = string.explode(msg, "|");
            -- print("id:"..info.id..", msgList:"..iif(msgList, #msgList, 0));
            info.p1 = msgList;
        end

        -- 解析坐标
        if str ~= "" then
            local _, _, x, y = string.find(str, "%(([-%d]+),[ ]*([-%d]+)%)");
            if x ~= nil and y ~= nil then
                info.p3 = { ["x"] = x, ["y"] = y, };
            else
                print("parse error! info.id:"..info.id..", str:"..str);
            end
        end
    end

    return info;
end

-- 获取配置信息
function query(id, path)
    local m = guideConfig[id];

    if not m or path == "" or not path then
        return m;
    end

    return m[path];
end

-- 获取下一个引导ID
function getNextGuideId(curId)
    if curId == 0 then
        return 1;
    end

    return query(curId, "next_id");
end

-- 还原引导进度
function restoreGuideProgress()

end

-- 获取当前引导ID
function getCurGuideId()
    return ME.user.dbase:query("dungeon_guide_progress", 0);
end

-- 设置当前引导ID
function setCurGuideId(id)
    return ME.user.dbase:set("dungeon_guide_progress", id);
end

-- 引导是否完成
function isGuideFinished()
    if SKIP_DUNGEON_GUIDE == 1 then
        return true;
    end

    local curGuideId = getCurGuideId();
    local nextGuideId = getNextGuideId(curGuideId);
    if nextGuideId == nil then
        return true;
    end

    return query(nextGuideId, "next_id") == nil;
end

